package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ManySheetOneSyler;
import com.jy.wms.pojo.WmsProductVelocity;
import com.jy.wms.service.WmsProductVelocityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 周转率报表
 * Created by syh on 2018/5/8.
 */
@Controller
@RequestMapping("/wmsvelocity")
@Api(value = "/wmsvelocity", description = "关于产品周转率报表")
public class WmsProductVelocityController extends BaseController<WmsProductVelocityService, WmsProductVelocity> {

    /**
     * 产品周转率报表 分页查询信息
     * @param wmsProductVelocity
     * @param session
     * @return
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "产品周转率报表 分页查询信息",response = JSONObject.class, notes = "POST产品周转率报表分页查询信息")
    public @ResponseBody JSONObject getVelocity(@RequestBody WmsProductVelocity wmsProductVelocity, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            Page<WmsProductVelocity> list= baseService.baseSelectPage(wmsProductVelocity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 产品周转率报表 导出信息
     * @param wmsProductVelocity
     * @param session
     * @param response
     * @throws IOException
     */
    @PostMapping(value = "VelocityExport")
    @ApiOperation(value = "产品周转率报表 导出信息", response = JSONObject.class, notes = "产品周转率报表 导出信息")
    public @ResponseBody void getVelocityExport(@RequestBody WmsProductVelocity wmsProductVelocity, HttpSession session, HttpServletResponse response) throws IOException {

        String fileName = "产品周转率报表信息";
        List<WmsProductVelocity> WmsProductVelocityList = null;
        try {
            WmsProductVelocityList = baseService.getVelocityExport(wmsProductVelocity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 文件名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("产品周转率报表信息", "UTF-8") + ".xls");
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsProductVelocity.class, WmsProductVelocityList);
        workbook.write(response.getOutputStream());
    }
}